package com.dependencies.quartzschedulermysql.scheduler;

import com.dependencies.quartzschedulermysql.jobs.SampleJob;
import jakarta.annotation.PostConstruct;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zhizhou   2024/8/7 23:23
 */
@Configuration
public class JobScheduler {
    
    @Autowired
    private Scheduler scheduler;
    
    //@Bean
    @PostConstruct
    public void scheduleJob() throws SchedulerException {
        JobDetail jobDetail = JobBuilder.newJob(SampleJob.class)
                                      .withIdentity("sampleJob")
                                      .storeDurably()
                                      .build();
        
        Trigger trigger = TriggerBuilder.newTrigger()
                                  .withIdentity("sampleTrigger")
                                  .forJob(jobDetail)
                                  .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                                                        .withIntervalInSeconds(10)//10秒执行一次
                                                        .repeatForever())
                                  .build();
        
        scheduler.scheduleJob(jobDetail, trigger);
    }
    
}
